﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameEngine.Camera;

namespace GameEngine.Scenes
{
    public class ScrollingBackground
    {
        private Texture2D backgroundTexture;
        private Camera2D camera;
        private int initialX;
        private int initialY;
        private GraphicsDevice device;
        private Rectangle rect;
        private Game game;

        /// <summary>
        /// Initializes a new instance of the <see cref="ScrollingBackground" /> class.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="backgroundTexture">The background texture.</param>
        /// <param name="game">The game.</param>
        public ScrollingBackground(GraphicsDevice device, Texture2D backgroundTexture, Game game)
        {
            this.backgroundTexture = backgroundTexture;
            this.device = device;
            camera = (Camera2D)game.Services.GetService(typeof(Camera2D));
            initialX = (int)(camera.Pos.X  - backgroundTexture.Width * 0.5);
            initialY = (int)(camera.Pos.Y + device.Viewport.Height * 0.5 - backgroundTexture.Height * 0.5);
            rect = new Rectangle(initialX, initialY, backgroundTexture.Width, backgroundTexture.Height);
        }

        public void Update(float deltaX)
        {
            rect.X = (int)((camera.Pos.X - device.Viewport.Width * 0.5) * 0.9f);
            rect.Y = (int)((camera.Pos.Y - device.Viewport.Height * 0.5) * 0.95f);
        }

        public void Draw(SpriteBatch batch)
        {
            batch.Draw(backgroundTexture, rect, Color.White);
        }
    }
}
